package org.jeecg.modules.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.entity.RunnerMatch;
import org.jeecg.modules.entity.RunnerMatchInfo;
import org.jeecg.modules.entity.RunnerMatchPlayer;
import org.jeecg.modules.entity.RunnerMatchScore;
import org.jeecg.modules.service.IRunnerMatchInfoService;
import org.jeecg.modules.service.IRunnerMatchPlayerService;
import org.jeecg.modules.service.IRunnerMatchScoreService;
import org.jeecg.modules.service.IRunnerMatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.*;

/**
 * @author tanxy
 * @date 2023年01月05日 14:36
 */
@Api(tags = "赛事比赛白名单接口")
@Slf4j
@RestController
@RequestMapping("/matchwhite")
public class MatchWhiteController {
    @Autowired
    private IRunnerMatchService matchService;
    @Autowired
    private IRunnerMatchInfoService matchInfoService;
    @Autowired
    private IRunnerMatchPlayerService playerService;
    @Autowired
    private IRunnerMatchScoreService scoreService;

    @AutoLog(value = "比赛信息查询")
    @ApiOperation(value = "赛事清单", notes = "获取所有赛事的列表和组信息")
    @RequestMapping("/matchinfo")
    public Result matchinfo(){
        List<RunnerMatch> list = matchService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("match",list);
        List<Object> infoList = new ArrayList<>();
        for(RunnerMatch match:list){
            String id = match.getId();
            List<RunnerMatchInfo> runnerMatchInfos = matchInfoService.selectByMainId(id);
            infoList.add(runnerMatchInfos);
        }
        map.put("info",infoList);
        return Result.ok(map);
    }

    @AutoLog(value = "比赛选手信息查询")
    @ApiOperation(value = "比赛选手信息查询", notes = "根据赛事和组获取赛事选手信息")
    @RequestMapping("/players")
    public Result players(String matchId,String groupId){
        if(StrUtil.isNotEmpty(matchId)){
            QueryWrapper<RunnerMatchPlayer> playerWrapper = new QueryWrapper();
            playerWrapper.eq("matchid",matchId);
            if(StrUtil.isNotEmpty(groupId)){
                playerWrapper.eq("groupid",groupId);
            }
            List<RunnerMatchPlayer> list = playerService.list(playerWrapper);
            return Result.ok(list);
        }
        return Result.error("筛选信息不能为空!");
    }

    @AutoLog(value = "获取比赛排行榜")
    @ApiOperation(value = "获取比赛排行榜", notes = "根据比赛ID获取比赛排行旁,组为 A B C 编号查询")
    @RequestMapping("/ranklist")
    public Result rankinglist(String matchId,String groupId){
        List<String> query = new ArrayList<>();
        if(StrUtil.isNotEmpty(groupId)){
            query.add(groupId);
        }
        List<RunnerMatchPlayer> matchPlayers = playerService.selectByMainIdAndGroupId(matchId,query);
        List<Map<String,Object>> res = new ArrayList<>();
        for(RunnerMatchPlayer player:matchPlayers){
            Map<String,Object> resMap = new HashMap<>();
            QueryWrapper<RunnerMatchScore> wrapper = new QueryWrapper<>();
            wrapper.eq("matchid",matchId).eq("player",player.getPlayer());
            List<RunnerMatchScore> runnerMatchScores = scoreService.list(wrapper);
            resMap.put("play",player.getPlayer());
            resMap.put("playcode",player.getPcode());
            resMap.put("groupid",player.getGroupid());
            resMap.put("pstarttime",player.getPstarttime());
            resMap.put("score",player.getScore());
            resMap.put("scorecj",player.getScorecj());
            if(player.getPendtime()==null){
                resMap.put("updateTime",new Date());
            }else{
                resMap.put("updateTime",player.getPendtime());
            }
            if(runnerMatchScores.size()==0){
                resMap.put("updateTime",player.getUpdateTime());
            }else {
                for(RunnerMatchScore score:runnerMatchScores){
                    resMap.put(score.getTimePoint(),score.getCleantime());
                }
            }
            res.add(resMap);
        }
        List<Map<String, Object>> realList = new ArrayList<>();
        for (Map<String, Object> tmp:res){
            Date updateTime = (Date)tmp.get("updateTime");
            //yyyy-MM-dd HH:mm:ss
            Date pstarttime = (Date)tmp.get("pstarttime");
            if(ObjectUtil.isNotEmpty(updateTime)){
                String s = DateUtils.formatDate(pstarttime,"yyyy-MM-dd HH:mm:ss");
                tmp.put("starttime",s);
                realList.add(tmp);
            }
        }

        Collections.sort(realList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Date updateTime1 = (Date)o1.get("updateTime");
                Date updateTime2 = (Date)o2.get("updateTime");
                return updateTime1.compareTo(updateTime2);
            }
        });

        return Result.ok(realList);
    }

}
